﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 26/11/2013
namespace VietThuongLMS.Services
{
    public class LienHeService
    {
        public static LienHeViewModel GetLienHeList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<LienHe> list = context.LienHes.Where(w => w.Deleted != Constant.LIENHE_DELETED)
                    .OrderBy(c => c.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.LienHes.Count();
                return new LienHeViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetLienHeList", ex.StackTrace, ex.Message);
                return new LienHeViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertLienHe(LienHe comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToLienHes(comp);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LienHeServices::InsertLienHe", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static LienHe GetLienHeById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.LienHes.Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LienHeServices::GetLienHeById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteLienHe(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                LienHe obj = context.LienHes.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    context.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LienHeServices::DeleteLienHe", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

    }
}